﻿namespace ETestIT.UI
{
    using System;
    using System.Web.UI.WebControls;
    using Core;
    using Core.Infrastructure;
    using Presentation;
    using Presentation.Storage;
    using SharpArch.Core.PersistenceSupport;

    public partial class Positions : PageBase<Positions>
    {
        private IRepository<Position> positionRepository;
        private ISessionProvider sessionProvider;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionProvider = IoC.Resolve<ISessionProvider>();
            positionRepository = IoC.Resolve<IRepository<Position>>();
            positionAddEdit.Update += PositionAddEditUpdate;

            if (!IsPostBack)
            {
                GetAllPositions();
            }
        }

        private void GetAllPositions()
        {
            gvPositions.DataSource = positionRepository.GetAll();
            gvPositions.DataBind();
        }

        protected void PositionAddEditUpdate(object sender, EventArgs e)
        {
            With.Transaction(() =>
                                 {
                                     var position = new Position();

                                     if (sessionProvider.Contains(Const.SelectedPositionKey))
                                     {
                                         var positionId = sessionProvider.Get<int>(Const.SelectedPositionKey);
                                         position = positionRepository.Get(positionId);
                                         sessionProvider.Remove(Const.SelectedPositionKey);
                                     }

                                     positionAddEdit.UpdateModel(position);
                                     positionRepository.SaveOrUpdate(position);

                                     GetAllPositions();

                                     Dialog.Information = "The positions list was updated successifully";
                                 });
        }

        protected void GridCommand(object sender, CommandEventArgs e)
        {
            int commandId = int.Parse(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "PositionView":
                    {
                        var thePosition = positionRepository.Get(commandId);
                        positionAddEdit.ShowModel(thePosition);
                    }
                    break;
                case "PositionNew":
                case "PositionEdit":
                    {
                        var thePosition = positionRepository.Get(commandId) ?? new Position();
                        positionAddEdit.ShowModel(thePosition);

                        if (commandId != 0)
                            sessionProvider.Set(Const.SelectedPositionKey, commandId);
                    }
                    break;
                case "PositionDelete":
                    {
                        With.Transaction(() =>
                        {
                            positionRepository.Delete(positionRepository.Get(commandId));
                            Dialog.Information = "The position was deleted successifully";
                        });

                        GetAllPositions();
                    }
                    break;
            }
        }
    }
}